<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="1000x">
        <el-form-item label="物料名称" prop="itemName">
          <el-input
            v-model="queryParams.itemName"
            placeholder="请输入物料名称模糊查询"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 250px"
          />
        </el-form-item>

      <el-form-item label="物料编码" prop="itemNum">
        <el-input
          v-model="queryParams.itemNum"
          placeholder="请输入物料编码模糊查询"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 250px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:workingHours:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" stripe :data="nonconformityList" :highlight-current-row="true" >
      <el-table-column label="序号" align="center" width="80" v-if="columns[0].visible">
        <template slot-scope="scope">
          <div>
            {{scope.$index+1}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="物料名称" align="center" prop="itemName"  :show-overflow-tooltip="true"/>
      <el-table-column label="物料编码" align="center" prop="itemNum"  :show-overflow-tooltip="true"/>
      <el-table-column label="规格" align="center" prop="itemSpecification" width="120"/>
      <el-table-column label="检验数量" align="center" prop="qcQuantity"/>
      <el-table-column label="不合格品数量" align="center" prop="failQuantity" />
      <el-table-column  align="center" label="不合格品占比(%)" prop="noNcPercent" >
        <template slot-scope="scope">
          {{ fun(scope.row.noNcPercent) }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listNonconformity } from "@/api/qms/nonconformity";
import { listUser} from "@/api/system/user";
import { listDept } from "@/api/system/dept";
export default {
  name: "Nonconformity",
  dicts: ['mes_dict_process_name'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 不合格品表格数据
      userNameList: [],
      deptList:[],
      nonconformityList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 不合格品占比时间范围
      daterangeDtBegin: [],
      // 不合格品占比时间范围
      daterangeDtEnd: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        processName: null,
        deptName: null,
        masterName: null,
        dtBegin: null,
        dtEnd: null,
        noncPercent: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
      }, // 列信息
      columns: [
        { key: 0, label: `序号`, visible: true },
        { key: 1, label: `物料名称`, visible: true },
        { key: 2, label: `工序名称`, visible: true },
        { key: 3, label: `班组`, visible: true },
        { key: 4, label: `负责人`, visible: true },
        { key: 5, label: `起始日期`, visible: true },
        { key: 6, label: `结束日期`, visible: true },
        { key: 7, label: `不合格品数量`, visible: true },
        { key: 8, label: `检验数量`, visible: true },
        { key: 9, label: `不合格品占比`, visible: true },
      ],
    };
  },
  created() {
    this.getList_userName()
    this.getList();
    this.getList_dept();
  },
  methods: {
    fun(val){
      return Number(val).toFixed(2);
    },
    getList_userName(){
      listUser().then(response => {
        this.userNameList = response.rows
      });
    },
    /** 列表: 部门（班组） */
    getList_dept(){
      /**班组数据下拉列表查询*/
      this.getConfigKey("workers_group_id").then(response => {
        console.log(response)
        this.queryParams.parentId = response.msg
        listDept(this.queryParams).then(response => {
          console.log(response)
          this.deptList = response.data
        });
      });
    },
    /** 查询不合格品列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if( this.queryParams.dtRange ){
        this.queryParams.dtDoneStart = this.queryParams.dtRange[0]
        this.queryParams.dtDoneEnd = this.queryParams.dtRange[1]
      }else {
        this.queryParams.dtDoneStart = null
        this.queryParams.dtDoneEnd = null
      }
      listNonconformity(this.queryParams).then(response => {
        this.nonconformityList = response.rows;
        console.log(this.nonconformityList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noncUuid: null,
        itemName: null,
        processName: null,
        deptName: null,
        masterName: null,
        dtBegin: null,
        noncQty: null,
        qcQty: null,
        dtEnd: null,
        noncPercent: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDtBegin = [];
      this.daterangeDtEnd = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('qms/nonconformity/export', {
        ...this.queryParams
      }, `qms_nonconformity.xlsx`)
    }
  }
};
</script>
